<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>axios请求方法别名</h1>
    <button id="btnGet">发送查询get请求</button>
    <button id="btnPost">发送新增post请求</button>
    <button id="btnUpdate">发送更新update请求</button>
    <button id="btnDelete">发送删除delete请求</button>
    <script>
        const oBtnGet = document.getElementById('btnGet');
        const oBtnPost = document.getElementById('btnPost');
        const oBtnUpdate = document.getElementById('btnUpdate');
        const oBtnDelete = document.getElementById('btnDelete');
        oBtnGet.onclick = async function () {
            const re = await axios.get("/user", {
                params: {
                    userId: "001"
                }
            })

            console.log(re.data);
        }
        oBtnPost.onclick = async function () {
            const re = await axios.post("/adduser", {
                name: "laowang",
                age: 18,
                sex: "男"
            })

            console.log(re.data);
        }
        oBtnUpdate.onclick = async function () {
            const re = await axios.put("/updateuser", {
                userId: "001",
                age: 18,
                sex: "女"
            })

            console.log(re.data);
        }
        oBtnDelete.onclick = async function () {
            const re = await axios.delete("/deleteuser", {
                params: {
                    userId: "001"
                }
            })
            console.log(re.data);
        }
        app.put("/updateuser", (req, res) => {
            const {
                userId,
                age,
                sex
            } = req.body;

            console.log(userId, age, sex, "更新");

            res.send({
                code: 10000,
                mes: "更新成功",
                data: null
            })
        })
        app.delete("/deleteuser", (req, res) => {
            const {
                userId
            } = req.query;
            console.log(userId, "删除");

            res.send({
                code: 10000,
                mes: "删除成功",
                data: null
            })
        })
        //静态资源部署接口
        app.get("/1", (req, res) => {
            const filePath = path.resolve(__dirname, "./01.axios的基础使用.html");
            res.sendFile(filePath);
        })

        app.get("/2", (req, res) => {
            const filePath = path.resolve(__dirname, "./02.axios请求别名.html");
            res.sendFile(filePath);
        })





        app.listen(9090, () => {
            console.log("服务器请求成功 请访问 http://127.0.0.1:9090");
        })
    </script>
</body>

</html>